package configuration

import (
	"encoding/json"
	"errors"
	"fmt"
	"kop/modules/configuration/client"
	"kop/util/log"
)

const (
	RuleTypeEveryone RuleType = 0 // 每一条都要
	RuleTypeRandOne  RuleType = 1 // 抽一条出来
	RuleTypeRemove   RuleType = 2 // 移除被选中的

	AwardTypeCoin AwardType = 1 // 银币
	//AwardTypeRoleExp AwardType = 2 // 主角声望
	AwardTypeItem AwardType = 3 // 道具
	AwardTypeRing AwardType = 4 // 魔戒
	AwardIncome   AwardType = 5 // 银币每秒收益的倍数
)

func init() {

	var err error
	if err = json.Unmarshal(client.Load("AwardTemplate"), &AwardConfigs); err != nil {
		panic(err)
	}

	log.Info("Load AwardTemplate.json success.")

	//********************************************************************//

	if err = json.Unmarshal(client.Load("AwardBag"), &AwardBagConfigs); err != nil {
		panic(err)
	}

	log.Info("Load AwardBag.json success.")

	//********************************************************************//

	if err = json.Unmarshal(client.Load("AwardContent"), &AwardContentConfigs); err != nil {
		panic(err)
	}

	log.Info("Load AwardContent.json success.")

}

type (
	RuleType  uint8
	AwardType uint8

	AwardConfig struct {
		ID       int32   // 奖励ID
		AwardBag []int32 // 奖励包组
	}

	AwardBagConfig struct {
		ID           int32    // 奖励包ID
		Weight       float32  // 随机权重
		Count        int32    // 掉落次数
		LevelMin     int32    // 角色最小等级
		LevelMax     int32    // 角色最大等级
		RuleType     RuleType // 规则类型
		AwardContent []int32  // 奖励内容组
	}

	ContentConf struct {
		ID        int32     // 奖励内容ID
		Weight    float32   // 权重
		AwardType AwardType // 奖励类型
		PropID    int32     // 奖励参数
		CountMin  int64     // 最小数量
		CountMax  int64     // 最大数量
	}
)

func (c ContentConf) GetID() int32 {
	return c.ID
}

func (c ContentConf) GetWeight() int32 {
	return int32(c.Weight)
}

//
//func (c ContentConf) RandInt32() int32 {
//
//	if c.CountMin == c.CountMax {
//		return int32(c.CountMin)
//	}
//
//	return int32(c.CountMin + rand.Int63n(c.CountMax-c.CountMin+1))
//}
//
//func (c ContentConf) RandInt64() int64 {
//
//	if c.CountMin == c.CountMax {
//		return c.CountMin
//	}
//
//	return c.CountMin + rand.Int63n(c.CountMax-c.CountMin+1)
//}

var (
	AwardConfigs        = map[int32]AwardConfig{}
	AwardBagConfigs     = map[int32]AwardBagConfig{}
	AwardContentConfigs = map[int32]ContentConf{}
)

func GetByAwardID(ID int32) (AwardConfig, error) {

	var conf, ok = AwardConfigs[ID]
	if ok {
		return conf, nil
	}

	return AwardConfig{}, errors.New(fmt.Sprintf("AwardID not found [%d]", ID))
}

func GetByAwardBagID(ID int32) (AwardBagConfig, error) {

	var conf, ok = AwardBagConfigs[ID]
	if ok {
		return conf, nil
	}

	log.Error("AwardBagID not found [%d]", ID)

	return AwardBagConfig{}, errors.New(fmt.Sprintf("AwardBagID not found [%d]", ID))
}

func GetByContentID(ID int32) (ContentConf, error) {

	var conf, ok = AwardContentConfigs[ID]
	if ok {
		return conf, nil
	}

	return ContentConf{}, errors.New(fmt.Sprintf("ContentID not found [%d]", ID))
}
